import APIS from "@/api";
export const getBase64 = (file: any): Promise<string> =>
  new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result as string);
    reader.onerror = (error) => reject(error);
  });
export async function uploadToAliyunOSS(file: any, config?: any) {
  // 'https://etontest.kafamily.cn/api/crm/' 'https://crm.etonkids.com/api/crm/')

  let data: any = await APIS.request.post(
    `${"https://etontest.kafamily.cn/api/crm/"}ali/getOssSignature`,
    { type: "img" },
    {
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
    }
  );

  const fileName = data.dir + `${Date.now()}-${file.name}`;
  let formData = new FormData();
  formData.append("success_action_status", "200");
  formData.append("policy", data.policy);
  formData.append("expire", data.expire);
  formData.append("signature", data.signature);
  formData.append("OSSAccessKeyId", data.accessid);
  formData.append("key", fileName); // 文件名
  formData.append("file", file); // file 必须为最后一个表单域
  await fetch(data.host, {
    method: "POST",
    body: formData,
  });
  return data.host + "/" + fileName;
}
